package com.wandian.news.mvvm.toutiao.toutiaodetailsother;

import android.app.Application;
import android.content.Context;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.google.gson.JsonObject;
import com.mylib.libcore.AppContext;
import com.mylib.libcore.bean.BaseBean;
import com.mylib.libcore.data.TTBaseAppApi;
import com.mylib.libcore.mvvm.BaseViewModel;
import com.mylib.libcore.network.rx.RxResultHelper;
import com.mylib.libcore.utils.JsonUtil;
import com.mylib.libcore.utils.RetrofitUtil;
import com.mylib.libcore.utils.ToastUtil;
import com.mylib.libcore.utils.UIUtils;
import com.mylib.libcore.utils.crc.AESUtils;
import com.mylib.widget.CustomCoinToast;
import com.wandian.news.bean.TouTiaoContentBean;
import com.wandian.news.data.BaseAppApi;

import java.util.HashMap;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

import static com.mylib.libcore.config.AppConfig.TOUTIAO_KEY;
import static com.mylib.libcore.config.NetConfig.BASE_SUCCESS_CODE;
import static com.mylib.libcore.config.NetConfig.BASE_TOU_TIAO;


/**
 * @author Administrator
 * @date 2019/12/18-16:11
 * @description TouTiaoDetailOtherViewModel
 **/
public class TouTiaoDetailOtherViewModel extends BaseViewModel {
    private TTBaseAppApi baseAppApiTT;
    public boolean isReFlash =true;
    private BaseAppApi baseAppApi;
    public MutableLiveData<TouTiaoContentBean> touTiaoContentBean =  new MutableLiveData<>();
    public TouTiaoDetailOtherViewModel(@NonNull Application application) {
        super(application);
        baseAppApiTT =  RetrofitUtil.getTouTiaoRetrofit().create(TTBaseAppApi.class);
        baseAppApi = RetrofitUtil.getAppRetrofit().create(BaseAppApi.class);
    }


    /**
     * 阅读奖励
     */
    public void readMoney(Context context){
        JsonObject jsonObject = new JsonObject();
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("advertName", "阅读奖励");
        hashMap.put("sourceType", "read");
        hashMap.put("remark", "");
        jsonObject.addProperty("wdk", AESUtils.getInstance().encrypt(hashMap));
        RxResultHelper.getHttpObservable(getLifecycleProvider(), baseAppApi.readMoney(jsonObject))
                .subscribe(new Observer<BaseBean<Object>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                    }
                    @Override
                    public void onNext(BaseBean<Object> bean) {
                        Log.d("upFightNum",   JsonUtil.toJson(bean));
                        if(!UIUtils.isEmpty(bean) && bean.getCode()==BASE_SUCCESS_CODE){
                            if(!UIUtils.isEmpty(bean.getMsg()) && bean.getMsg().equals("今日金币奖励已上限")){
                                ToastUtil.show("今日金币奖励已上限");
                            }else {
                                int num = AppContext.getInstance().getAppPreferencesHelper().getSubmitTaskNum();
                                AppContext.getInstance().getAppPreferencesHelper().setSubmitTaskNum(num+1);
                                CustomCoinToast.showToast(context,"阅读奖励","0.1金币");
                            }
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public void getContentData(String value, int page, boolean isReFlash){
        if(UIUtils.isEmpty(value)){
            touTiaoContentBean.setValue(null);
            return;
        }

        this.isReFlash = isReFlash;
        JsonObject jsonObject = new JsonObject();
        String url = BASE_TOU_TIAO+value+"/index";
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("key",TOUTIAO_KEY);
        hashMap.put("num","50");
        hashMap.put("page",page+"");
        RxResultHelper.getHttpObservableNothing(getLifecycleProvider(), baseAppApiTT.getTouTiaoContent(url,hashMap,jsonObject))
                .subscribe(new Observer<Object>() {

                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(Object o) {

                        if(!UIUtils.isEmpty(o)){
                            Log.d("getTouTiaoContent", JsonUtil.toJson(o)+"");
                            TouTiaoContentBean bean = JsonUtil.parseJsonToBean(JsonUtil.toJson(o),TouTiaoContentBean.class);
                            if(!UIUtils.isEmpty(bean) && bean.getCode()==200){
                                touTiaoContentBean.setValue(bean);
                            }
                        }

                    }

                    @Override
                    public void onError(Throwable e) {

                        ToastUtil.show("网络错误");
                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }


}
